
<script type="text/javascript">
$(document).ready(function(){
    var options = { 
        type:"POST",
        dataType : "text",
        buttons:$('#mainForm').find("button[name='button']"),
        async : true,
        cache: false,
        global:true,
        success:function(responseText, statusText)
        {
            var json = jQuery.parseJSON(responseText);
            if(json.result != 1)
            {
                dialogMessage(json.messages);
            }
            pageSelector(0);
            
            dialogTip(json.messages, function(){});
            return "";
        },
    };            
    jQuery("#mainForm").validate({
        debug: true,
        messages: {
            content: "<?php echo $this->translate('Reply content is required.');?>",
        },
        errorPlacement: function(error, element) { 
            var elementName = element.attr("name");
            var idTip = 'tip-' + elementName;
            $("#" + idTip).html(error);
        },
        invalidHandler: function(form, validator) {
            var message = joiner = "";
            $.each(validator.invalid,function(key,value){
                message += joiner + value;
                joiner = "<br />";
            });
            dialogMessage(message);
        },
        success: function(label) {
            label.addClass("valid").text("Ok!")
        },
        submitHandler: function(form) {
            jQuery(form).ajaxSubmit(options);
        }
     });

    function pageselectCallback(page_id, jq) {
        pageSelector(page_id);
    }

    function pageSelector(page_index){
        $.ajax({   
            type: "get",   
            url: "<?php echo $this->url(array('module'=>'blog', 'controller'=>'reply', 'action'=>'list-by-json'),'default',true) ?>", 
            data: {page: page_index+1, articleId: '<?php echo $this->article->articleId;?>'},
            beforeSend: function() {   
            },   
            success:function(responseText, statusText){
                //alert(responseText);
                var json = jQuery.parseJSON(responseText);

                if(json.result == 1){
                     $("#replies").html(json.content);
                }
                    
                return true;
            }, 
        });
        
        return false;
    }
    
    $("#pagination").pagination('<?php echo $this->paginator->getTotalItemCount();?>', {
        num_edge_entries: 2,
        num_display_entries: 8,
        callback: pageselectCallback,
        items_per_page: '<?php echo $this->paginator->getItemCountPerPage();?>'
    });
});
</script>

<?php 
    echo $this->headLink()
        ->appendStylesheet('/ui/style/base/'.$this->settings['styleBase'].'/reply.css')
    ;
?>

<div class="container">
    <div class="column span-18 colborder">
        <?php 
        echo $this->partial('script_article.phtml', array(
             'article'  => $this->article,
        ));            
        ?>

        <hr class="reply">
        
        <h3 class="title"><?php echo $this->translate('Replies');?></h3>
        <div id="reply-box" class="clearfix">
            <form method="post" name="mainForm" id="mainForm" action="<?php echo $this->url(array('module'=>'publish', 'controller'=>'reply', 'action'=>'save-post'), "default", true) ?>">
            <input type="hidden" name="destArticleId" value="<?php echo $this->article->articleId;?>" />
            <div class="">
                <textarea id="content" name="content" class="span-18 ui-corner-all required"></textarea>
            </div>
            <div class="">
                <label id="tip-content" for="content" generated="true" class="" />请填写评论内容</label>
            </div>
            <div class="">
                <input type="submit" name="submit" id="submit" value=" <?php echo $this->translate('Submit');?> " />
            </div>
            </form>
        </div>
        
        <div id="replies" class="clearfix">
            <?php foreach($this->replies as $key=>$article){?>
            <div class="prepend-top append-bottom">
                <?php 
                echo $this->partial('script_article-with-avatar.phtml', array(
                        'article'    => $article,
                ));            
                ?>
            </div>
            <hr>
            <?php }?>
        </div>
        
        <div class="paginator" id="pagination"><?php echo $this->paginationControl($this->paginator, 'Elastic', 'script_pagelist.phtml'); ?></div>
        
    </div>
    
    <div class="column span-5 last">
        <?php 
        echo $this->partial('module_blog_script_sidebar.phtml', array(
             'author'       => $this->author,
             'isMyBuddy'    => $this->isMyBuddy,
        ));
        ?>
    </div>
    
</div>

